<?php
/**
* @version $Id: mospicasaweb.php, v0.1 $
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
* @Author: gyufi - http://www.direction.hu
*
*
**/

/** Ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$_MAMBOTS->registerFunction( 'onPrepareContent', 'newPicasaweb' );

function newPicasaweb( $published, &$row, &$params, $page=0 ) {
  global $database;

  // Load the Bot's params (all)
  $query = "SELECT id FROM #__mambots WHERE element = 'mospicasaweb' AND folder = 'content'";
  $database->setQuery( $query );
  $id = $database->loadResult();
  $mambot = new mosMambot( $database );
  $mambot->load( $id );
  $botparams =& new mosParameters( $mambot->params );   // Use "$botparms" to avoid overwritting "$params" (from Module)



  if (preg_match('{mospicasaweb:.*}', $row->text)) {
	if ($published) {  // Bot Published => replace {mospicasaweb:} with embedded code
		$mosaddpicasaweb_entrytext = $row->text;		
		if (preg_match_all('/{mospicasaweb:user=([^,]+),album=([^}]+)}/', $mosaddpicasaweb_entrytext, $mosaddpicasaweb_matches, PREG_PATTERN_ORDER) > 0) {
/*
echo "<pre>";
var_dump($mosaddpicasaweb_matches);
echo "</pre>";
*/
			for ($i=0; $i <= count($mosaddpicasaweb_matches[0]); $i++) {
				$user  = $mosaddpicasaweb_matches[1][$i];
				$album = $mosaddpicasaweb_matches[2][$i];
				$mosaddpicasaweb_entrytext = preg_replace("/{mospicasaweb:[^}]+}/", embedded_code($user,$album), $mosaddpicasaweb_entrytext, 1);
			}
			$row->text = $mosaddpicasaweb_entrytext;
		
		}
	} else {           // Bot not Published => remove {mospicasaweb:.*}
	  $row->text = preg_replace ("/{mospicasaweb:[^}]+}/", "", $row->text);
	}
}
  return true;
}

function embedded_code($user,$album) {
	$str = '<embed type="application/x-shockwave-flash"
			src="http://picasaweb.google.com/s/c/bin/slideshow.swf"
			width="600" height="400"
			flashvars="host=picasaweb.google.com&captions=1&RGB=0x000000&feed=http%3A%2F%2Fpicasaweb.google.com%2Fdata%2Ffeed%2Fapi%2Fuser%2F'.$user.'%2Falbum%2F'.$album.'%3Fkind%3Dphoto%26alt%3Drss"
			pluginspage="http://www.macromedia.com/go/getflashplayer">
		</embed>';
	return $str;
}
